package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import control.LoginController.LoginAction;

public class ClientLoginWindow implements ActionListener {

	private JFrame frmLogin;
	private JButton sendButton;
	private JTextField textField;

	private LoginAction la;

	public ClientLoginWindow(LoginAction la) {
		initialize();
		this.la = la;
	}

	public void show() {
		this.frmLogin.setVisible(true);
	}

	private void initialize() {
		frmLogin = new JFrame();
		frmLogin.setResizable(false);
		frmLogin.setTitle("Chat");
		frmLogin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmLogin.setBounds(100, 100, 250, 64);
		frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmLogin.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Login:");
		lblNewLabel.setBounds(10, 11, 46, 14);
		frmLogin.getContentPane().add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(50, 8, 86, 20);
		frmLogin.getContentPane().add(textField);
		textField.setColumns(10);
		
		this.sendButton = new JButton("Login");
		this.sendButton.setBounds(146, 7, 89, 23);
		frmLogin.getContentPane().add(this.sendButton);
		this.sendButton.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		try {
		    if (e.getSource() == this.sendButton) {
		    	String login = this.textField.getText().trim();
		    	
		    	if(!(login.equals("") || login.equalsIgnoreCase("Servidor") || login.equalsIgnoreCase("Bate-papo") )) {
		    		la.login(login);
			    	this.frmLogin.dispose();
		    	}
		    }
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
}
